﻿using mediaControl.Event;
using mediaControl.Utils;
using mediaControl.Vo;
using Newtonsoft.Json;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;

namespace mediaControl.ViewModels
{
    public class MainViewModel : BindableBase
    {
        private readonly IRegionManager _regionManger;

        //事件聚合器
        private readonly IEventAggregator _eventAggregator;

        private string title = "嘟答智能播放系统";

        private ProgramVo program;

        private string saveFlag = "1";

        private DispatcherTimer timer = new DispatcherTimer();

        public string Title { get { return title; } set { this.title = value; RaisePropertyChanged(); } }

        public string SaveFlag { get { return saveFlag; } set { this.saveFlag = value; RaisePropertyChanged(); } }
        public MainViewModel(IRegionManager regionManager, IEventAggregator eventAggregator)
        {
            _regionManger = regionManager;
            _eventAggregator = eventAggregator;
            _regionManger.RegisterViewWithRegion("MainRegion", "HomePage");
            _eventAggregator.GetEvent<OpenMainWindowEvent>().Subscribe(Recive);
            _eventAggregator.GetEvent<CloseWindowEvent>().Subscribe(ChangeSaveFlag);
            _eventAggregator.GetEvent<ChangeTitleEvent>().Subscribe(ChangeTitle);
            timer.Interval = TimeSpan.FromSeconds(30);
            
        }

        private void ChangeTitle(ProgramVo obj)
        {
            if (null != obj)
            {
                SaveFlag = "0";
                program = obj;
                Title = "嘟答智能播放系统\\" + obj.Name + "\\" + obj.FilePath;
            }
        }

        private void ChangeSaveFlag(string obj)
        {
            SaveFlag = obj;
        }

        private List<string> regions = new List<string>() { "Monitor", "Monitor1", "Monitor2", "Monitor3", "Monitor4" };

        private List<string> screenViews = new List<string>() { "Screen", "Screen1", "Screen2", "Screen3", "Screen4" };

        private void Recive(ProgramVo obj)
        {
            if (null != obj)
            {
                SaveFlag = "0";
                program = obj;
                Title = "嘟答智能播放系统\\" + obj.Name + "\\" + obj.FilePath;
                //打开主界面
               
               
               // _eventAggregator.GetEvent<ProgramEvent>().Publish(obj);
                //处理屏幕
                /*
                string filePath = "D:\\screenlist.info";
                if (File.Exists(filePath))
                {
                    string content = WriteFileUtil.readFile(filePath);
                    ObservableCollection<ScreenVo> screens = new ObservableCollection<ScreenVo>();
                    if (!String.IsNullOrEmpty(content))
                    {
                        screens = JsonConvert.DeserializeObject<ObservableCollection<ScreenVo>>(content);
                        if (null != screens && screens.Count > 0)
                        {
                            List<String> screenIds = new List<string>();
                            foreach (ScreenVo screen in screens)
                            {
                                screenIds.Add(screen.Id + "");
                            }
                            _eventAggregator.GetEvent<OpenScreenEvent>().Publish(screenIds);
                        }
                    }
                }*/
                initScreenList();
                NavigationParameters keys = new NavigationParameters();
                keys.Add("program", obj);
                keys.Add("screenIds", screenIds);
                keys.Add("monitorVos", monitorVos);
                _regionManger.RequestNavigate("MainRegion", "MainWindow",keys);
            }
        }
        List<MonitorVo> monitorVos = new List<MonitorVo>();
        List<String> screenIds = new List<string>();
        private void initScreenList()
        {
            
            string filePath = AppDomain.CurrentDomain.BaseDirectory + "/Config/screenlist.info";
            if (File.Exists(filePath))
            {
                ObservableCollection<ScreenVo> screens = new ObservableCollection<ScreenVo>();
                string content = WriteFileUtil.readFile(filePath);
                if (!String.IsNullOrEmpty(content))
                {
                    screens = JsonConvert.DeserializeObject<ObservableCollection<ScreenVo>>(content);
                }
               
                //打开屏幕
                if (null != screens && screens.Count > 0)
                {
                    screenIds = new List<string>();
                    monitorVos = new List<MonitorVo>();
                    for (int i = 0; i < screens.Count; i++)
                    {
                        
                        MonitorVo monitorVo = new MonitorVo();
                        monitorVo.Screen = screens[i];
                       
                        string region = regions[0];
                        string screenView = screenViews[0];

                        screenIds.Add(screens[i].Id + "");
                        switch (region)
                        {
                            case "Monitor":
                                //屏幕监控
                                monitorVo.Monitor = "Monitor";
                                regions.Remove(region);
                                break;
                            case "Monitor1":
                               
                                monitorVo.Monitor = "Monitor1";
                                regions.Remove(region);
                                break;
                            case "Monitor2":
                               
                                monitorVo.Monitor = "Monitor2";
                                regions.Remove(region);
                                break;
                            case "Monitor3":
                               
                                monitorVo.Monitor = "Monitor3";
                                regions.Remove(region);
                                break;
                            case "Monitor4":
                                monitorVo.Monitor = "Monitor4";
                                regions.Remove(region);
                                break;
                        }
                        monitorVos.Add(monitorVo);
                    }
                    //_eventAggregator.GetEvent<GetMonitorEvent>().Publish(monitorVos);
                    //_eventAggregator.GetEvent<OpenScreenEvent>().Publish(screenIds);
                }
            }
        }
    }
}
